package cn.sh.tools.network;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author code4crafter@gmail.com <br>
 */
public class ImgPageEndProcessor implements PageProcessor {

    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000);
    private static final String destinationPath = "D:/10-downloadFile/01-edge/";

    @Override
    public Site getSite() {
        return site;
    }

    @Override
    public void process(Page page) {
        String imgHref = page.getUrl().get();
        String destinationFile = destinationPath + imgHref.substring(imgHref.lastIndexOf("/") + 1);
        try (FileOutputStream outputStream = new FileOutputStream(destinationFile)) {
            // 文件下载
            outputStream.write(page.getBytes());
        } catch (IOException ignored) {
        }
    }

}
